package appx.craft.droid.ui;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import appx.craft.droid.R;
import appx.craft.droid.uc.ItemSaveListener;
import appx.craft.droid.uc.PopUpSaveItem;
import appx.craft.droid.utils.Log;

public class AddItemActivity extends FragmentActivity implements ItemSaveListener{
	
	private final String TAG = AddItemActivity.class.getCanonicalName();
	private ArrayList<String> mTempItemList;
	private LinearLayout horizontial_gallery;
	
	@Override
	protected void onCreate(Bundle saveInstance) {
		super.onCreate(saveInstance);
		setContentView(R.layout.activity_add_items);
		Log.d(TAG, "[ onCreate ]");
		
		mTempItemList = new ArrayList<String>();
		mTempItemList.add("ADD");
		
		horizontial_gallery = (LinearLayout)findViewById(R.id.horizontial_gallery);
		
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				
				for(int i = 0;i<mTempItemList.size();i++){
					if(i ==0){
						horizontial_gallery.addView(AddNewItems(mTempItemList.get(i),true));
					}else{
						horizontial_gallery.addView(AddNewItems(mTempItemList.get(i), false));
					}
					
				}
			}
		});

		
		
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "[ onStart ]");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "[ onResume ]");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "[ onPause ]");
	}
	
	
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "[ onRestart ]");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "[ onStop ]");
	}

	@Override
	public void onItemSaveClick(final String item) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				horizontial_gallery.addView(AddNewItems(item, false));
			}
		});
		
	}
	
	public View AddNewItems(String itemName,boolean isAddButton){
		
		final LayoutInflater mLayoutInflater = LayoutInflater.from(AddItemActivity.this);
		final View mConvertView = mLayoutInflater.inflate(R.layout.listview_addnew_item,null);
		
		final TextView mTextView_itemName = (TextView)mConvertView.findViewById(R.id.textview_itemName);
		mTextView_itemName.setText(itemName);
		ImageView mImageViewData = (ImageView)mConvertView.findViewById(R.id.imageviewdata);
		Button btn_Add_new_item = (Button)mConvertView.findViewById(R.id.btn_new_item_add);
		if(isAddButton){
			mImageViewData.setVisibility(View.GONE);
			btn_Add_new_item.setVisibility(View.VISIBLE);
			btn_Add_new_item.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					new PopUpSaveItem(AddItemActivity.this).show();
				}
			});
		}else{
			
			mImageViewData.setVisibility(View.VISIBLE);
			btn_Add_new_item.setVisibility(View.GONE);
		}
		
		
		return mConvertView;
	}
	
	
}
